package com.cskaoyan.bean.cart.vo;

import com.cskaoyan.bean.Cart;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.math.BigDecimal;
import java.util.List;

@Data
public class CartData {

    /**
     * cartTotal : {"goodsCount":2,"checkedGoodsCount":1,"goodsAmount":1035,"checkedGoodsAmount":999}
     * cartList : [{"id":83,"userId":1,"goodsId":1116011,"goodsSn":"1116011","goodsName":"蔓越莓曲奇 200克","productId":167,"price":36,"number":1,"specifications":["标准"],"checked":false,"picUrl":"http://yanxuan.nosdn.127.net/767b370d07f3973500db54900bcbd2a7.png","addTime":"2021-06-07 16:01:10","updateTime":"2021-06-07 16:07:59","deleted":false},{"id":87,"userId":1,"goodsId":1181000,"goodsSn":"1181000","goodsName":"母亲节礼物-舒适安睡组合","productId":1,"price":999,"number":1,"specifications":["1.5m床垫*1+枕头*2","浅杏粉"],"checked":true,"picUrl":"http://yanxuan.nosdn.127.net/1f67b1970ee20fd572b7202da0ff705d.png","addTime":"2021-06-07 16:09:38","updateTime":"2021-06-07 16:09:38","deleted":false}]
     */

    private CartTotalBean cartTotal;
    private List<Cart> cartList;

    public CartData() {
    }
    public CartData(List<Cart> cartList) {
        this.cartList = cartList;
        generateCartTotalData(cartList);
    }

    //统计购物车数据
    private void generateCartTotalData(List<Cart> cartList) {
        if (cartList == null) {
            return;
        }
        Integer goodsCount = cartList.size();
        Integer checkedGoodsCount = 0;
        BigDecimal goodsAmount = BigDecimal.ZERO;
        BigDecimal checkedGoodsAmount = BigDecimal.ZERO;
        for (Cart cart : cartList) {
            BigDecimal price = cart.getPrice();
            if (cart.getChecked()) {
                checkedGoodsCount++;
                checkedGoodsAmount = checkedGoodsAmount.add(price);
            }
            goodsAmount = goodsAmount.add(price);
        }
        this.cartTotal = new CartTotalBean(goodsCount, checkedGoodsCount, goodsAmount, checkedGoodsAmount);
    }


    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class CartTotalBean {
        /**
         * goodsCount : 2
         * checkedGoodsCount : 1
         * goodsAmount : 1035
         * checkedGoodsAmount : 999
         */

        private Integer goodsCount;
        private Integer checkedGoodsCount;
        private BigDecimal goodsAmount;
        private BigDecimal checkedGoodsAmount;


    }


}
